package com.tange.ai.iot.client.gnssa.extensions;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import androidx.annotation.Nullable;

import com.tange.base.toolkit.DimenUtil;
import com.tange.base.toolkit.GlobalApplicationContext;
import com.tange.core.ability.VideoCapability;

import java.util.ArrayList;
import java.util.List;

public class CropVideoCapability extends VideoCapability {
    public static float CROP_RATIO = 0.6f;
    public static float LINE_INTERVAL = 2.5f;
    public static int lineInterval = 0;

    @Override
    public void init(@Nullable Context context) {
        lineInterval = DimenUtil.dp2px(GlobalApplicationContext.application(), LINE_INTERVAL);
    }

    @Override
    public Bitmap process(Bitmap bitmap) {
        if (bitmap == null) return null;
        int cropSingleWidth = (int) (bitmap.getWidth() * CROP_RATIO);
        Bitmap leftBmp = Bitmap.createBitmap(bitmap, 0, 0, cropSingleWidth, bitmap.getHeight());
        Bitmap rightBmp = Bitmap.createBitmap(bitmap, bitmap.getWidth() - cropSingleWidth, 0, cropSingleWidth,
                bitmap.getHeight());
        // 画布
        int width = leftBmp.getWidth() + rightBmp.getWidth();
        int height = Math.max(leftBmp.getHeight(), rightBmp.getHeight());
        Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(result);
        // 绘制左边图像
        canvas.drawBitmap(leftBmp, 0, 0, null);
        // 绘制右边图像
        canvas.drawBitmap(rightBmp, leftBmp.getWidth(), 0, null);
        leftBmp.recycle();
        rightBmp.recycle();
        // 绘制白线间隔
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(lineInterval);
        int startX = (width - lineInterval) / 2;
        canvas.drawLine(startX, 0, startX + lineInterval, height, paint);
        return result;
    }

    public static List<Bitmap> getCropBitmaps(Bitmap bitmap) {
        int lineInterval = DimenUtil.dp2px(GlobalApplicationContext.application(), LINE_INTERVAL);
        int cropSingleWidth = (bitmap.getWidth() - lineInterval) / 2;
        Bitmap leftBmp = Bitmap.createBitmap(bitmap, 0, 0, cropSingleWidth, bitmap.getHeight());
        Bitmap rightBmp = Bitmap.createBitmap(bitmap, leftBmp.getWidth() + lineInterval, 0, cropSingleWidth,
                bitmap.getHeight());
        List<Bitmap> bitmaps = new ArrayList<>();
        bitmaps.add(leftBmp);
        bitmaps.add(rightBmp);
        return bitmaps;
    }

    @Nullable
    @Override
    public String name() {
        return "crop-video";
    }

    @Override
    public void destroy() {
    }
}
